<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/img/login_icon.png" alt="" />
        <span class="title">黑马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <el-form class="userform" ref="form" :model="form" :rules="rules">
        <!-- 手机号验证 -->
        <el-form-item style="width:100%" prop="phone">
          <el-input
            prefix-icon="el-icon-user"
            v-model="form.phone"
            placeholder="请输入手机号"
          >
          </el-input>
        </el-form-item>
        <!-- 密码验证 -->
        <el-form-item style="width:100%" prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="form.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 验证码验证 -->
        <el-row class="regbox">
          <el-col :span="16"
            ><div class="grid-content bg-purple">
              <el-form-item style="width:100%" prop="code">
                <el-input
                  prefix-icon="el-icon-key"
                  v-model="form.code"
                  placeholder="请输入验证码"
                ></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="7"
            ><div class="grid-content bg-purple-light">
              <img
                class="regimg"
                :src="httpAddres"
                alt=""
                @click="changImg"
              /></div
          ></el-col>
        </el-row>
        <!-- 用戶协议 -->
        <el-form-item prop="checked">
          <el-checkbox v-model="form.checked"></el-checkbox
          >&nbsp;我已阅读并同意<el-link
            type="primary"
            href="http://www.baidu.com"
            target="__blank"
            >用户协议</el-link
          >和<el-link type="primary" href="http://www.jd.com" target="__blank"
            >隐私条款</el-link
          >
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm" style="width:100%"
            >登录</el-button
          >
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            style="width:100%"
            @click="changeDialogVisible"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/login_bg.png" alt="" />
    </div>
    <register ref="register"></register>
  </div>
</template>

<script>
import { setKey } from '@/untils/token.js'
import register from './register'
export default {
  name: 'Link',
  components: {
    register
  },
  data () {
    return {
      form: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        checked: true
      },
      httpAddres: process.env.VUE_APP_BASEURL + '/captcha?type=login',
      rules: {
        phone: [
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                return callback(new Error('请输入手机号'))
              }
              const reg = /^1[3-9][0-9]{9}$/
              if (!reg.test(value)) {
                return callback(new Error('请输入正确的手机号'))
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        code: [
          {
            validator: (rule, value, callback) => {
              const reg = /^\d+$/
              if (!reg.test(value)) {
                return callback(new Error('请输入正确的验证码'))
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('必须勾选用户协议'))
              }
              callback()
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    changeDialogVisible () {
      this.$refs.register.dialogVisible = true
    },
    changImg () {
      this.httpAddres =
        process.env.VUE_APP_BASEURL + '/captcha?type=login&a=' + Math.random()
    },
    submitForm () {
      // async写在异步代码函数前
      this.$refs.form.validate(async valid => {
        // console.log(valid)
        if (!valid) return this.$message('请输入完整')

        // this.$axios.post('/login', this.form).then(res => {
        //   //   console.log(res)
        //   if (res.code === 200) {
        //     this.$message.success('登录成功~')
        //     localStorage.setItem('heimamm64Token', res.data.token)
        //     this.$router.push('/layout')
        //   }
        // })

        const res = await this.$axios.post('/login', this.form)
        if (res.code === 200) {
          this.$message.success('登录成功~')
          // localStorage.setItem('heimamm64Token', res.data.token)
          setKey(res.data.token)
          this.$router.push('/layout')
        } else {
          // console.log(res)
          this.$message.error(res.message)
          //   刷新验证码
          this.changImg()
        }
      })
    }
  }
}
</script>

<style lang="less">
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .userform {
      margin-top: 29px;
      .regbox {
        margin-bottom: 22px;
        .regimg {
          width: 100%;
          height: 40px;
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
